const AllObject: UiImage[] = [];
console.clear();
const foundObject = UiScreen.getAllScreen().find(
  (obj) => obj.name === 'screen'
);
const value = {
  木剑: 0,
  石剑: 25,
  铁剑: 50,
  钻石剑: 100,
  下界合金剑: 125,
  金剑: 175,
  羊毛: 5,
  木板: 10,
  砂岩: 15,
  末地石: 25,
  火焰弹: 100,
  金苹果: 150,
  治疗药水: 250,
  末影珍珠: 150,
  '弓箭-1': 200, //上传这个的时候名字写错了
  箭: 20,
  铁盔甲: 100,
  钻石盔甲: 150,
  下界合金盔甲: 200,
};
const buyNumber = {
  木剑: 1,
  石剑: 1,
  铁剑: 1,
  钻石剑: 1,
  下界合金剑: 1,
  金剑: 1,
  羊毛: 16,
  木板: 16,
  砂岩: 16,
  末地石: 12,
  火焰弹: 2,
  金苹果: 2,
  治疗药水: 1,
  末影珍珠: 2,
  '弓箭-1': 1, //上传这个的时候名字写错了
  箭: 5,
  铁盔甲: 1,
  钻石盔甲: 1,
  下界合金盔甲: 1,
};
const allItem: string[] = [];
for (const valueEach in value) {
  allItem.push(valueEach);
}
class createGood {
  name: string;
  parent: UiRenderable;
  constructor(name: string, parent: UiRenderable) {
    this.name = name;
    this.parent = parent;
  }
  createImage() {
    if (!allItem.includes(this.name)) {
      return;
    }
    const a = UiImage.create();
    a.parent = this.parent;
    a.position.offset.copy(Vec2.create({ x: 5, y: 5 }));
    a.size.offset.x = this.parent.size.offset.x * 0.8;
    a.size.offset.y = this.parent.size.offset.y * 0.8;
    a.image = `picture/${this.name}.png`;
    a.cancancel = true;
    a.backgroundOpacity = 0;
    this.parent.type = this.name;
  }
  createValue() {
    if (!allItem.includes(this.name)) {
      return;
    }
    const a = UiText.create();
    a.parent = this.parent;
    a.position.offset.copy(Vec2.create({ x: 5, y: 5 }));
    a.textContent = String(value[this.name as keyof typeof value]);
    a.size.offset.copy(Vec2.create({ x: 5, y: 5 }));
    a.cancancel = true;
  }
  createNumber(number: number) {
    if (!allItem.includes(this.name)) {
      return;
    }
    const a = UiText.create();
    a.parent = this.parent;
    a.position.offset.copy(Vec2.create({ x: 40, y: 40 }));
    a.textContent = `*${number}`;
    a.size.offset.copy(Vec2.create({ x: 5, y: 5 }));
    a.textStrokeColor.copy(Vec3.create({ r: 0, g: 0, b: 0 }));
    a.textColor.copy(Vec3.create({ r: 255, g: 255, b: 255 }));
    a.textStrokeThickness = 2.5;
    a.cancancel = true;
  }
}
function cancelAllUI() {
  world.inShopping = false;
  foundObject?.children.forEach((x) => {
    const ui = x as UiRenderable;
    if (ui.cancancel) {
      ui.parent = undefined;
    }
  });
}

function cancelAllThis(ui: UiRenderable) {
  foundObject?.children.forEach((x) => {
    if (!x.children.length) {
      return;
    }
    x.children.forEach((y) => {
      const ui = y as UiRenderable;
      if (ui.cancancel) {
        ui.parent = undefined;
      }
    });
  });
}
class fullScreen {
  #ui: UiRenderable;
  constructor(ui: UiRenderable) {
    this.#ui = ui;
  }
  size_fullScreen() {
    this.#ui.size.offset.copy(Vec2.create({ x: screenWidth, y: screenHeight }));
  }
  position_fullScreen() {
    this.#ui.position.offset.copy(
      Vec2.create({ x: screenWidth / 2, y: screenHeight / 2 })
    );
  }
}
const under = foundObject?.findChildByName('快捷栏') as UiImage;
under.position.offset.copy(
  Vec2.create({ x: screenWidth / 2 - 250, y: screenHeight - 50 })
);
under.size.offset.copy(Vec2.create({ x: 500, y: 50 }));
remoteChannel.onClientEvent(async (event) => {
  if (event.type === 'playerDataLoaded') {
    const loadingBox = foundObject?.findChildByName('loadingBox') as UiBox;
    if (loadingBox) {
      loadingBox.visible = true;
      new fullScreen(loadingBox).position_fullScreen();
      new fullScreen(loadingBox).size_fullScreen();
    }
    const loadingtext = foundObject?.findChildByName('loadText') as UiText;
    if (loadingtext) {
      loadingtext.textContent = 'loading';
      loadingtext.visible = true;
      new fullScreen(loadingtext).position_fullScreen();
      let a = 3;
      while (a) {
        loadingtext.textContent += '.';
        a--;
        await sleep(750);
      }
    }
    await sleep(500);
    loadingtext.visible = false;
    if (loadingBox) {
      while (loadingBox.backgroundOpacity > 0) {
        loadingBox.backgroundOpacity -= 0.1;
        await sleep(64);
      }
    }
    loadingBox.visible = false;
  }
  if (event.type === 'market') {
    if (world.inShopping) {
      return;
    }
    world.inShopping = true;
    const marketBox = foundObject?.findChildByName('购买装备') as UiImage;
    if (marketBox) {
      let xIndex: number = 0,
        yIndex: number = 0;
      for (
        let h = screenHeight / 2 - 175;
        h < screenHeight / 2 + 175;
        h += 50
      ) {
        yIndex++;
        for (
          let i = screenWidth / 2 - 225;
          i < screenWidth / 2 + 225;
          i += 50
        ) {
          xIndex++;
          const item = UiImage.create();
          item.parent = foundObject;
          item.position.offset.copy(Vec2.create({ x: i, y: h }));
          item.size.offset.copy(Vec2.create({ x: 50, y: 50 }));
          item.cancancel = true;
          item.imageIndex = xIndex;
          if (xIndex === 10) {
            const a = UiImage.create();
            a.parent = foundObject;
            a.position.offset.copy(item.position.offset);
            a.size.offset.copy(item.size.offset);
            a.image = 'picture/钻石剑.png';
            a.backgroundOpacity = 0;
            a.cancancel = true;
            item.type = '武器UI';
          }
          if (xIndex === 12) {
            const a = UiImage.create();
            a.parent = foundObject;
            a.position.offset.copy(item.position.offset);
            a.size.offset.copy(item.size.offset);
            a.image = 'picture/箱子.png';
            a.backgroundOpacity = 0;
            a.cancancel = true;
            item.type = '道具UI';
          }
          if (xIndex === 14) {
            const a = UiImage.create();
            a.parent = foundObject;
            a.position.offset.copy(item.position.offset);
            a.size.offset.copy(item.size.offset);
            a.image = 'picture/羊毛.png';
            a.backgroundOpacity = 0;
            a.cancancel = true;
            item.type = '方块UI';
          }
          if (xIndex === 16) {
            const a = UiImage.create();
            a.parent = foundObject;
            a.position.offset.copy(item.position.offset);
            a.size.offset.copy(item.size.offset);
            a.image = 'picture/弓箭-1.png';
            a.backgroundOpacity = 0;
            a.cancancel = true;
            item.type = '弓箭UI';
          }
          if (xIndex === 18) {
            const a = UiImage.create();
            a.parent = foundObject;
            a.position.offset.copy(item.position.offset);
            a.size.offset.copy(item.size.offset);
            a.image = 'picture/钻石盔甲.png';
            a.backgroundOpacity = 0;
            a.cancancel = true;
            item.type = '盔甲UI';
          }
          if (!item.image) {
            item.image = 'picture/格子.png';
          }
          item.events.add('pointerdown', ({ target }) => {
            if (target.type === '武器UI') {
              cancelAllThis(target);
              foundObject?.children.forEach((x) => {
                const ui = x as UiRenderable;
                if (ui.imageIndex === 28) {
                  const create = new createGood('石剑', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                } else if (ui.imageIndex === 30) {
                  const create = new createGood('铁剑', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                } else if (ui.imageIndex === 32) {
                  const create = new createGood('钻石剑', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                } else if (ui.imageIndex === 34) {
                  const create = new createGood('下界合金剑', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                } else if (ui.imageIndex === 36) {
                  const create = new createGood('金剑', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                }
              });
            } else if (target.type === '方块UI') {
              cancelAllThis(target);
              foundObject?.children.forEach((x) => {
                const ui = x as UiRenderable;
                if (ui.imageIndex === 29) {
                  const create = new createGood('羊毛', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                } else if (ui.imageIndex === 31) {
                  const create = new createGood('木板', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                } else if (ui.imageIndex === 33) {
                  const create = new createGood('砂岩', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                } else if (ui.imageIndex === 35) {
                  const create = new createGood('末地石', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                }
              });
            } else if (target.type === '道具UI') {
              cancelAllThis(target);
              foundObject?.children.forEach((x) => {
                const ui = x as UiRenderable;
                if (ui.imageIndex === 30) {
                  const create = new createGood('火焰弹', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                } else if (ui.imageIndex === 32) {
                  const create = new createGood('治疗药水', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                } else if (ui.imageIndex === 34) {
                  const create = new createGood('末影珍珠', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                }
              });
            } else if (target.type === '弓箭UI') {
              cancelAllThis(target);
              foundObject?.children.forEach((x) => {
                const ui = x as UiRenderable;
                if (ui.imageIndex === 31) {
                  const create = new createGood('弓箭-1', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                } else if (ui.imageIndex === 33) {
                  const create = new createGood('箭', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                }
              });
            } else if (target.type === '盔甲UI') {
              cancelAllThis(target);
              foundObject?.children.forEach((x) => {
                const ui = x as UiRenderable;
                if (ui.imageIndex === 30) {
                  const create = new createGood('铁盔甲', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                } else if (ui.imageIndex === 32) {
                  const create = new createGood('钻石盔甲', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                } else if (ui.imageIndex === 34) {
                  const create = new createGood('下界合金盔甲', ui);
                  create.createImage();
                  create.createValue();
                  create.createNumber(
                    buyNumber[create.name as keyof typeof buyNumber]
                  );
                }
              });
            } else {
              if (target.type) {
                remoteChannel.sendServerEvent({
                  type: '购买',
                  data: `${target.type}*${buyNumber[target.type as keyof typeof buyNumber]}`,
                });
              }
            }
          });
        }
      }
    }
  }
  if (event.type === '玩家背包') {
    if (world.inShopping || world.bagOpen) {
      world.inShopping = false;
      world.bagOpen = false;
      cancelAllUI();
    }
    world.bagOpen = true;
    let xIndex = 0;
    let bag_xIndex = 0,
      bag_yIndex = 0;
    for (let h = screenHeight / 2 - 100; h < screenHeight / 2 + 100; h += 50) {
      bag_yIndex++;
      bag_xIndex = 0;
      for (let i = screenWidth / 2 - 225; i < screenWidth / 2 + 225; i += 50) {
        xIndex++;
        bag_xIndex++;
        const item = UiImage.create() as UiImage;
        item.parent = foundObject;
        item.position.offset.copy(Vec2.create({ x: i, y: h }));
        item.size.offset.copy(Vec2.create({ x: 50, y: 50 }));
        item.cancancel = true;
        item.imageIndex = xIndex;
        item.xIndex = bag_yIndex;
        item.yIndex = bag_xIndex;
        if (!item.image) {
          item.image = 'picture/格子.png';
        }
        const data = event.data as Array<Array<string>>;
        item.type = data[bag_yIndex - 1][bag_xIndex - 1];
        const create = new createGood(
          data[bag_yIndex - 1][bag_xIndex - 1].split('*')[0],
          item
        );
        create.createImage();
        create.createNumber(
          Number(data[bag_yIndex - 1][bag_xIndex - 1].split('*')[1])
        );
        item.events.add('pointerdown', ({ target }) => {
          remoteChannel.sendServerEvent({
            type: '选择物品',
            data: [item.xIndex, item.yIndex, item.type],
          });
        });
      }
    }
  }
  if (event.type === '快捷栏') {
    if (!under.children.length) {
      let index = 0;
      for (
        let startx = screenWidth / 2 - 250;
        startx < screenWidth / 2 + 200;
        startx += 50
      ) {
        const a = UiImage.create();
        a.position.offset.copy(Vec2.create({ x: 12 + index * 50, y: 10 }));
        a.size.offset.copy(Vec2.create({ x: 30, y: 30 }));
        a.parent = under;
        a.backgroundOpacity = 0;
        a.xIndex = index;
        const b = UiText.create();
        b.parent = a;
        b.textContent =
          event.data[index].split('*')[1] !== '0'
            ? event.data[index].split('*')[1]
            : '';
        b.size.offset.copy(Vec2.create({ x: 5, y: 5 }));
        b.position.offset.copy(Vec2.create({ x: 30, y: 30 }));
        b.size.offset.copy(Vec2.create({ x: 5, y: 5 }));
        b.textStrokeColor.copy(Vec3.create({ r: 0, g: 0, b: 0 }));
        b.textColor.copy(Vec3.create({ r: 255, g: 255, b: 255 }));
        b.textStrokeThickness = 2.5;
        if (event.data) {
          if (
            event.data[index].split('*')[0] &&
            event.data[index].split('*')[1]
          ) {
            a.image = `picture/${event.data[index].split('*')[0]}.png`;
          } else {
            a.image = '';
            a.imageOpacity = 0;
          }
        }
        index++;
        a.events.add('pointerdown', () => {
          remoteChannel.sendServerEvent({
            type: '快捷选择',
            data: a.xIndex,
          });
        });
      }
    } else {
      const data = event.data as string[];
      let index = 0;
      for (const x of data) {
        const a = under.children[8 - index] as UiImage;
        if (x.split('*')[0] === '' || !x.split('*')[1]) {
          a.image = '';
          a.imageOpacity = 0;
          const b = a.children[0] as UiText;
          b.textContent = '';
        } else {
          a.image = `picture/${x.split('*')[0]}.png`;
          a.imageOpacity = 1;
          const b = a.children[0] as UiText;
          b.textContent =
            event.data[index].split('*')[1] !== '0'
              ? event.data[index].split('*')[1]
              : '';
          b.size.offset.copy(Vec2.create({ x: 5, y: 5 }));
          b.position.offset.copy(Vec2.create({ x: 30, y: 30 }));
          b.size.offset.copy(Vec2.create({ x: 5, y: 5 }));
          b.textStrokeColor.copy(Vec3.create({ r: 0, g: 0, b: 0 }));
          b.textColor.copy(Vec3.create({ r: 255, g: 255, b: 255 }));
          b.textStrokeThickness = 2.5;
        }
        index++;
      }
    }
  }
  if (event.type === '初始化生命') {
    let index = 0;
    for (let x = screenWidth / 2 - 250; x < screenWidth / 2; x += 25) {
      index++;
      const a = UiImage.create();
      a.parent = foundObject;
      a.position.offset.x = x;
      a.position.offset.y = screenHeight - 75;
      a.backgroundOpacity = 0;
      a.size.offset.copy(Vec2.create({ x: 25, y: 25 }));
      a.image = 'picture/生命.png';
      a.xIndex = index;
      a.type = 'health';
    }
  }
  if (event.type === '生命') {
    if (event.data % 2 === 0) {
      foundObject?.children
        .filter((x) => x.type === 'health')
        .forEach((x) => {
          const a = x as UiImage;
          if (a.xIndex === event.data / 2) {
            a.image = 'picture/生命.png';
          }
          if (a.xIndex > event.data / 2) {
            a.image = 'picture/空生命.png';
          }
          if (a.xIndex < event.data / 2) {
            a.image = 'picture/生命.png';
          }
        });
    }
    if (event.data % 2 !== 0) {
      foundObject?.children
        .filter((x) => x.type === 'health')
        .forEach((x) => {
          const a = x as UiImage;
          if (a.xIndex === event.data / 2 + 0.5) {
            a.image = 'picture/半颗心.png';
          }
          if (a.xIndex > event.data / 2 + 0.5) {
            a.image = 'picture/空生命.png';
          }
          if (a.xIndex < event.data / 2 + 0.5) {
            a.image = 'picture/生命.png';
          }
        });
    }
  }
  if (event.type === 'coin') {
    const text = foundObject?.findChildByName<UiText>('金币') as UiText;
    text.textContent = `你有${event.data}个金币`;
  }
  if (event.type === '关闭UI') {
    if (world.inShopping || world.bagOpen) {
      world.inShopping = false;
      world.bagOpen = false;
      cancelAllUI();
    } else {
      remoteChannel.sendServerEvent({
        type: '打开背包',
      });
    }
  }
});
const cancelAll = foundObject?.findChildByName('关闭') as UiImage;
cancelAll.events.add('pointerdown', () => {
  if (world.inShopping || world.bagOpen) {
    world.inShopping = false;
    world.bagOpen = false;
    cancelAllUI();
  } else {
    remoteChannel.sendServerEvent({
      type: '打开背包',
    });
  }
});
const start = foundObject?.findChildByName('start') as UiImage;
start.position.offset.copy(Vec2.create({ x: 0, y: 75 }));
start.events.add('pointerdown', () => {
  remoteChannel.sendServerEvent({
    type: '选择操作',
  });
});
